﻿using ACC.Data.Entities.Common;
using ACC.Extensions.Entities;
using ACC.Extensions.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ACC.Extensions.PCClient.UserControls
{
    /// <summary>
    /// Interaction logic for BarcodeCountControl.xaml
    /// </summary>
    public partial class BarcodeCountControl : UserControl
    {
        public ClientBomItems BomItem { get; set; }

        public List<string> ScannedBarcode { get; set; }

        public int ScannedCount { get; set; }

        public bool IsFull
        {
            get
            {
                //Log4Helper.Info(string.Format("IsFull: ScannedBarcode.Count ={0}, BomItem.Qty ={1}, BomItem.IDName = {2}",
                //    ScannedBarcode.Count.ToString(), BomItem.Qty.ToString(), BomItem.IDName));
                return ScannedBarcode.Count >= int.Parse(string.IsNullOrEmpty(BomItem.Qty) ? "0" : BomItem.Qty);
            }
        }
        public BarcodeCountControl(ClientBomItems bomItem)
        {
            InitializeComponent();
            this.BomItem = bomItem;
            ScannedBarcode = new List<string>();

            txtBarcodeName.Text = BomItem.IDName;
            txtRequireCount.Text = BomItem.Qty.ToString();
            txtBarcodeCount.Text = "0";
        }


        /// <summary>
        /// 检验条码是否和表达式匹配
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public bool IsMatch(string barcode)
        {
            bool bRet = false;
            if (String.IsNullOrEmpty(BomItem.IDPattern))
            {
                //没有配置表达式时，无论什么格式的条码都能通过
                bRet = true;
            }
            else
            {
                bRet = Regex.IsMatch(barcode, BomItem.IDPattern);
            }
            Log4Helper.Info(string.Format("IsMatch: barcode ={0}, IDPattern ={1}, Result={2}", 
                barcode, BomItem.IDPattern, bRet));

            return bRet;
        }

        public bool AddBarcode(string barcode)
        {
            if (IsFull)
            {
                return false;
            }
            Dispatcher.Invoke(new Action(() =>
            {
                if (!ScannedBarcode.Contains(barcode))
                {
                    ScannedBarcode.Add(barcode);
                    ScannedCount++;
                    txtBarcodeCount.Text = ScannedCount.ToString();
                }
            }));
            return true;
        }
        public bool AddBarcode(string barcode, out ClientBomItems bom)
        {
            bom = BomItem;
            if (IsFull)
            {
                return false;
            }
            Dispatcher.Invoke(new Action(() =>
            {
                if (!ScannedBarcode.Contains(barcode))
                {
                    ScannedBarcode.Add(barcode);
                    ScannedCount++;
                    txtBarcodeCount.Text = ScannedCount.ToString();
                }
            }));
            return true;
        }

        public void RemoveBarcode(string barcode)
        {
            string foundBarcode = ScannedBarcode.FirstOrDefault<string>(b => b == barcode);
            if (!String.IsNullOrEmpty(foundBarcode))
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    ScannedBarcode.Remove(foundBarcode);
                    ScannedCount--;
                    if (ScannedCount <= 0)
                    {
                        ScannedCount = 0;
                    }
                    txtBarcodeCount.Text = ScannedCount.ToString();
                }));
            }
        }

        public void Reset()
        {
            ScannedCount = 0;
            ScannedBarcode.Clear();
            txtBarcodeCount.Text = ScannedCount.ToString();
        }
    }
}
